package com.information.manage.basic.dataOperator;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.information.manage.basic.model.entity.StaffDO;
import com.information.manage.basic.common.recycling.RecoveryDataOperateManager;
import com.information.manage.basic.common.recycling.RecoveryDataOperator;
import com.information.manage.basic.common.recycling.RecoveryException;
import com.information.manage.basic.common.utils.FileUtils;
import com.information.manage.basic.mapper.StaffMapper;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import javax.annotation.Resource;
import java.util.Collections;
import java.util.List;

@Component
public class StaffDataOperator implements RecoveryDataOperator {

    @Resource
    private StaffMapper staffMapper;

    @Resource
    private RecoveryDataOperateManager recoveryDataOperateManager;

    @Resource
    private FileUtils fileUtils;

    @PostConstruct
    public void init(){
        recoveryDataOperateManager.addDataOperate(this);
    }

    @Override
    public void deleteData(Class entityClass, List<Integer> ids) {
        //获取员工指纹路径
        List<StaffDO> staffDOList = staffMapper.selectList(new LambdaQueryWrapper<StaffDO>()
                .in(StaffDO::getId, ids));
        //删除员工的指纹及图片数据
        for (StaffDO staffDO : staffDOList) {
            if (staffDO.getFingerprint() != null && !"".equals(staffDO.getFingerprint().trim())) fileUtils.deleteFolder(staffDO.getFingerprint());
            if (staffDO.getIdCardImage() != null && !"".equals(staffDO.getIdCardImage().trim())) fileUtils.deleteFolder(staffDO.getIdCardImage());
        }
        //删除员工基本信息表数据
        int deleteCount = staffMapper.deleteBatchIds(ids);
        if (deleteCount != ids.size()) throw new RecoveryException("员工删除失败，员工ids = " + ids);
    }

    @Override
    public List<Class> dependencies() {
        return Collections.EMPTY_LIST;
    }

    @Override
    public Class getEntityClass() {
        return StaffDO.class;
    }
}